package com.javaee.reflection;

import java.lang.reflect.Method;

public class ReflectionExample {
    public static void main(String[] args) throws Exception {
        // 使用反射机制获取 Book 类的 Class 对象
        Class clz = Class.forName("com.javaee.reflection.Book");

        // 使用默认无参构造方法创建 Car 对象
        Object bookObj = clz.getConstructor().newInstance();

        // 获取 setTitle 方法，并使用反射设置属性值
        Method setTitle = clz.getMethod("setTitle", String.class);
        setTitle.invoke(bookObj,"活着");

        Method setAuthor = clz.getMethod("setAuthor", String.class);
        setAuthor.invoke(bookObj,"余华");

        Method setPrice = clz.getMethod("setPrice", Double.class);
        setPrice.invoke(bookObj,19.9);

        Method setPress = clz.getMethod("setPress", String.class);
        setPress.invoke(bookObj,"上海人民出版社");

        System.out.println("Book: " + bookObj);

    }
        
}
